import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Enumeration;

@WebServlet("/ZCServlet")
public class ZCServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");
        String pass=request.getParameter("pass");
        String repass=request.getParameter("repass");
        if (!pass.equals(repass)){
            request.setAttribute("login","密码与确认密码不同");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else {
            Connection conn = null;
            PreparedStatement pstm = null;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                String url = "jdbc:mysql:///student?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT&allowPublicKeyRetrieval=true";
                String user = "root";
                String pwd = "123456";
                conn = DriverManager.getConnection(url, user, pwd);
                String sql = "insert into admin(name,password) value (?,?)";
                pstm = conn.prepareStatement(sql);
                pstm.setString(1,name);
                pstm.setString(2,pass);
                int add = pstm.executeUpdate();
                System.out.println(add);
                request.getRequestDispatcher("login.jsp").forward(request,response);
                if (pstm != null) {
                    pstm.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
